package com.lst.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lst.api.constant.ApplyTypeEnum;
import com.lst.api.entity.*;
import com.lst.api.exception.RequestParamErrorException;
import com.lst.api.mapper.ProductMapper;
import com.lst.api.service.IApplyInfoService;
import com.lst.api.service.ILoanBankService;
import com.lst.api.service.IProductService;
import com.lst.api.service.ITaxInfoService;
import com.lst.api.vo.response.MatchProductResponse;
import com.lst.api.vo.response.ProductResponse;
import com.lst.tools.mybatis.service.impl.BaseService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 贷款产品表业务层实现
 *
 * @author Frank
 * @date 2019-12-30 10:31:37
 * @version: V1.0
 */
@Service
public class ProductService extends BaseService<ProductMapper, Product> implements IProductService {

    @Autowired
    private IApplyInfoService applyInfoService;
    @Autowired
    private ITaxInfoService taxInfoService;
    @Autowired
    private ILoanBankService loanBankService;

    @Override
    public List<MatchProductResponse> listMatchProductByApplyNo(String applyNo) {
        ApplyInfo applyInfo = applyInfoService.getByApplyNo(applyNo);
        if (applyInfo == null) {
            return Collections.EMPTY_LIST;
        }
        List<Product> productList = null;
        //企业
        if (ApplyTypeEnum.COMPANY.getKey().equals(applyInfo.getApplyType())) {
            Calendar calendar = Calendar.getInstance();
            int year = calendar.get(Calendar.YEAR) - 1;
            TaxInfo taxInfo = taxInfoService.getByApplyNoAndYear(applyNo, year);
            if (taxInfo == null) {
                throw new RequestParamErrorException("请填写最新年份的税务等级");
            }
            productList = this.listByTaxLevel(taxInfo.getTaxLevel());
        } else {//个人
            //todo 待确定
            productList = Collections.EMPTY_LIST;
        }
        if (CollectionUtils.isEmpty(productList)) {
            return Collections.EMPTY_LIST;
        }
        Map<Long, List<ProductResponse>> dataMap = new LinkedHashMap<>();
        List<Long> loanBankIdList = new ArrayList<>();
        for (Product product : productList) {
            Long loanBankId = product.getLoanBankId();
            loanBankIdList.add(loanBankId);

            List<ProductResponse> productResponses = dataMap.get(loanBankId);
            if (CollectionUtils.isEmpty(productResponses)) {
                productResponses = new ArrayList<>();
            }
            ProductResponse productResponse = new ProductResponse(product);
            productResponses.add(productResponse);
            dataMap.put(loanBankId, productResponses);
        }
        Map<Long, LoanBank> loanBankMap = loanBankService.getMapByIds(loanBankIdList);
        List<MatchProductResponse> responses = new ArrayList<>();
        for (Map.Entry<Long, List<ProductResponse>> entry : dataMap.entrySet()) {
            MatchProductResponse matchProductResponse = new MatchProductResponse();
            Long loanBankId = entry.getKey();
            matchProductResponse.setLoanBankId(loanBankId);
            LoanBank loanBank = loanBankMap.get(loanBankId);
            matchProductResponse.setLoanBankName(loanBank.getTitle());
            matchProductResponse.setProductList(entry.getValue());
            responses.add(matchProductResponse);
        }
        return responses;

    }

    @Override
    public List<Product> listByTaxLevel(String taxLevel) {
        if (StringUtils.isBlank(taxLevel)) {
            return Collections.EMPTY_LIST;
        }
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        StringBuilder like = new StringBuilder();
        like.append("%").append(taxLevel).append("%");
        queryWrapper
                .like(Product.TAX_LEVEL, like.toString())
                .orderByAsc(Product.MAX_QUOTA)
                .orderByDesc(Product.CREATE_AT)
                .orderByDesc(Product.ID);
        return super.list(queryWrapper);
    }
}